﻿
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace FinancialManagement.Infrstructre
{
    public class BaseRepository<T>:IRepository<T> where T:class,new()
    {
        private readonly ISqlSugarClient db;

        public BaseRepository(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(T t)
        {
            return await db.Insertable(t).ExecuteCommandAsync();
        }


        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<ISugarQueryable<T>> GetAllAsyns()
        {
            return db.Queryable<T>();
        }

        /// <summary>
        /// 没有异步获取数据
        /// </summary>
        /// <returns></returns>
        public List<T> QueryAll()
        {
            return db.Queryable<T>().ToList();
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> RemoveAsync(List<T> t)
        {
            return db.Deleteable(t).ExecuteCommand();
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateAsync(T t)
        {
            return await db.Updateable(t).ExecuteCommandAsync();
        }
    }
}
